<?php
namespace app\admin\controller;

use app\core\Incontroller;
use think\Request;

class Index extends Incontroller
{
    public function _empty($name){
        return $name;
    }

    public function index()
    {
        $user_menu = cache('cache_admin_user_id_'.session('admin_user_id'));
        $this->assign('menu',$user_menu['user_menu']);
        unset($user_menu);
        return $this->fetch('index/index');
    }

    public function login(){
        //判断是否已经登录
        if($this->_isLogin()){
            $this->redirect(url('admin/index/index'));
        }

        $request = Request::instance();
        //判断是否post提交
        if($request->isPost()){
            $user_name = strtolower(trim($request->post('user_name')));
            $pass_word = strtolower(trim($request->post('pass_word')));

            $admin = model('admin/admin');
            $res = $admin->login($user_name,$pass_word);

            $data = array(
                'err' => 1,
                'msg' => '用户不存在或密码错误！'
            );

            if($res){
                $data = array(
                    'err' => 0,
                    'msg' => '登录成功'
                );
            }

            return $data;
        }

        return $this->fetch('index/login');
    }

    public function logout(){  //退出登录
        cache('cache_admin_user_id_'.session('admin_user_id'),'');
        session(null);
        $this->redirect(url('admin/index/login'));
    }
}